<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-table :data="goodsList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品描述" prop="text"></el-table-column>
        <el-table-column label="商品价格" prop="oldPrice"></el-table-column>
        <el-table-column label="优惠价" prop="newPrice"></el-table-column>
        <el-table-column label="商品类型" prop="classify"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUser(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="10"
        layout="sizes, prev, pager, next"
        :total="totals"
      >
      </el-pagination>
    </el-card>
     <!-- 修改用户信息 -->
    <el-dialog
      title="修改用户信息"
      :visible.sync="editDialogVisible"
      width="50%" @close="editDialogClosed">
      <!-- form表单 输入框 -->
      <el-form :model="editForm"  ref="editFormRef" label-width="70px">
        <el-form-item label="用户名" prop="username" >
          <el-input v-model="editForm.username" disabled></el-input>
        </el-form-item>
         <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
         <el-form-item label="手机号" prop="mobile">
         <el-input v-model="editForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoods } from "@/network/request.js";
import { getPage } from "@/network/request.js";
import { getNumber } from "@/network/request.js";
import { getTotal } from "@/network/request.js";
export default {
  data() {
    return {
      goodsList: [],
      totals: null,
      addDialogVisible:false,
      editDialogVisible:false,
      editForm:{
          username:"",
          password:'',
          mobile:''
      }
    };
  },
  methods: {
    handleSizeChange(newSize) {
      console.log(newSize);
      var obj = {
        params: {
          num: newSize,
        },
      };
      getNumber(obj).then((data) => {
        this.goodsList = data.data;
        console.log(data);
      });
    },
    handleCurrentChange(newPage) {
      console.log(newPage);
      var obj = {
        params: {
          page: newPage,
        },
      };
      getPage(obj).then((data) => {
        this.goodsList = data.data;
        console.log(data);
      });
    },
    showEditDialog(id) {
      this.editDialogVisible = true;
      //拿到id发送请求  请求数据
      console.log(id);
      //这里就是手动演示一下  不是数据请求
      for (var i = 0; i < this.goodsList.length; i++) {
        if (this.goodsList[i].id == id) {
          console.log(this.goodsList[i]);
          this.editForm = this.goodsList[i];
        }
      }
    },
    removeUser(id) {
      const res = this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //在这里拿到id发送给服务器进行对应删除
          console.log(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log(res);
    },
     //关闭清空表单
    addDialogClosed(){
      this.$refs.addFormRef.resetFields()
    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields()
    },
    editUser(){
      this.$refs.editFormRef.validate(valid=>{
        if(!valid) return 
        //点击确认按钮
        //将this.editForm的数据发送给后端数据库
        //发送后再次的发送请求更新数据
        //最后在this.editDialogVisible = false关闭模态框
        console.log(valid)
        console.log(this.editForm)
        //这里要请求完了数据并修改完了之后在关闭模态框  不然就没有数据发送给后台
        //this.editDialogVisible = false;
      })
    },
  },
  beforeMount() {
    getGoods().then((data) => {
      this.goodsList = data.data;
      console.log(data);
    });
    getTotal().then((data) => {
      this.totals = data.data.length;
    });
  },
};
</script>

<style lang="less" scoped>
</style>